package com.ruoyi.main.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.main.domain.MainMaterial;
import com.ruoyi.main.service.IMainMaterialService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料管理Controller
 * 
 * @author chao
 * @date 2024-07-23
 */
@Controller
@RequestMapping("/main/material")
public class MainMaterialController extends BaseController
{
    private String prefix = "main/material";

    @Autowired
    private IMainMaterialService mainMaterialService;


    @RequiresPermissions("main:material:view")
    @GetMapping()
    public String material()
    {
        return prefix + "/material";
    }

    /**
     * 查询物料管理列表
     */
    @RequiresPermissions("main:material:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(MainMaterial mainMaterial)
    {
        startPage();
        List<MainMaterial> list = mainMaterialService.selectMainMaterialList(mainMaterial);
        return getDataTable(list);
    }

    /**
     * 导出物料管理列表
     */
    @RequiresPermissions("main:material:export")
    @Log(title = "物料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MainMaterial mainMaterial)
    {
        List<MainMaterial> list = mainMaterialService.selectMainMaterialList(mainMaterial);
        ExcelUtil<MainMaterial> util = new ExcelUtil<MainMaterial>(MainMaterial.class);
        return util.exportExcel(list, "物料管理数据");
    }

    /**
     * 新增物料管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存物料管理
     */
    @RequiresPermissions("main:material:add")
    @Log(title = "物料管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MainMaterial mainMaterial)
    {
        return toAjax(mainMaterialService.insertMainMaterial(mainMaterial));
    }

    /**
     * 修改物料管理
     */
    @RequiresPermissions("main:material:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MainMaterial mainMaterial = mainMaterialService.selectMainMaterialById(id);
        mmap.put("mainMaterial", mainMaterial);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料管理
     */
    @RequiresPermissions("main:material:edit")
    @Log(title = "物料管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MainMaterial mainMaterial)
    {
        return toAjax(mainMaterialService.updateMainMaterial(mainMaterial));
    }

    /**
     * 删除物料管理
     */
    @RequiresPermissions("main:material:remove")
    @Log(title = "物料管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mainMaterialService.deleteMainMaterialByIds(ids));
    }
}
